<template>
  <dialog-edit :visible.sync="dialogVisible" :title="$attrs.title" ref="userManageEdit" width="850" @save="save" :remember="false" :destroyOnClose="true"
               :height="500" :saveFlag="true" :loading="loading"
               :escClosable="true" :closeFlag="true" :saveLoading="saveLoading">
    <template v-slot:body>
      <div style="margin-bottom: 10px">
        <vxe-form :data="formData" :span="24" title-align="right" :rules="formRules"
                  :title-width="60" ref="formRefs">
          <div class="allq">
            <vxe-form-item title="标题" field="Name">
              <template v-slot>
                <!--            <vxe-input v-model="formData.name" placeholder="请选择分类" clearable></vxe-input>-->
                <vxe-input v-model="formData.Name" placeholder="请输入标题"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="时间" field="Started">
              <template v-slot>
                <!--            <vxe-input v-model="formData.name" placeholder="请选择分类" clearable></vxe-input>-->
                <vxe-input v-model="formData.Started" placeholder="请选择时间" type="date"></vxe-input>
              </template>
            </vxe-form-item>
          </div>
          <vxe-form-item title="描述" field="Desc">
            <template v-slot="scope">
              <vxe-textarea v-model="formData.Desc" placeholder="请输入描述" :autosize="{ minRows: 12, maxRows: 12 }" maxlength="200"></vxe-textarea>
            </template>
          </vxe-form-item>
        </vxe-form>
      </div>
    </template>
  </dialog-edit>
</template>

<script>
import dialogEdit from '@/components/element/dialogEdit'
import projectTaskApi from "@/api/projectTaskApi";
import * as format from "@/filters";
export default {
  name: "MilestoneDialog",
  components: {
    dialogEdit,
  },
  props: {
    visible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    preFormData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data () {
    return {
      formData: {},
      saveLoading: false,
      formRules: {
        Started: [
          { required: true, message: '时间必填', trigger: 'change' },
        ],
        Name: [
          { required: true, message: '标题必填', trigger: 'change' },
        ]
      },
      loading: false
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (value) {
        this.$emit('update:visible', value)
      }
    },
  },
  methods: {
    save () {
      this.saveLoading = true
      let a = Object.assign(this.formData, {
        Projectid: this.preFormData.Projectid,
        Started: new Date(this.formData.Started).valueOf(),
        Planid: this.preFormData.Planid,
        Level: '1',
        Moduleid: '1',
        Classify: '3'
      })
      if (a.Id) {
        a.id = a.Id
        projectTaskApi.update(a).then(res => {
          if (res) {
            if (res.code === 1) {
              this.success(res.message)
              this.dialogVisible = false
              this.$emit('close')
            } else {
              this.error(res.message)
            }
          }
        }).finally(() => {
          this.saveLoading = false
        })
      } else {
        projectTaskApi.create(a).then(res => {
          if (res) {
            if (res.code === 1) {
              this.success(res.message)
              this.dialogVisible = false
              this.$emit('close')
            } else {
              this.error(res.message)
            }
          }
        }).finally(() => {
          this.saveLoading = false
        })
      }
    },
  },
  watch: {
    dialogVisible: {
      handler (n, o) {
        this.formData = JSON.parse(JSON.stringify(this.preFormData))
        if (this.preFormData.Started) {
          let a = this.preFormData.Started.toString().length === 13 ? this.preFormData.Started : this.preFormData.Started * 1000
          this.$set(this.formData, 'Started', format.formatDatetime(this.preFormData.Started, 'yyyy-MM-dd'))
        }
      }
    }
  }
}
</script>

<style scoped>
.wange >>> .w-e-text{
  overflow-y: hidden;
}
.allq >>> .vxe-form--item {
  width: 100%;
}
</style>
